//
//  VRDeviceDataViewController.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/14.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRDeviceDataViewController.h"

#import "VRDeviceDataCell.h"
#import "XDropMenuView.h"

#import "VRDeviceDataModel.h"

@interface VRDeviceDataViewController () <UITableViewDelegate, UITableViewDataSource, XDropMenuViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) XDropMenuView *dropMenu;
@property (nonatomic, strong) UILabel *typeTitleLbl;

@property (nonatomic, strong) NSArray<VRDeviceDataModel *> *deviceDataModelArray;

@end

@implementation VRDeviceDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"数据详情";
    self.view.backgroundColor = ScrollViewBgColor;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setupSubView];
    [self setupHeaderView];
    [self setupListMenu];
    
    [self getDeviceType];
//    [self getDeviceData];
}

- (void)setupSubView
{
    self.tableView.y = NAVIGATION_BAR_HEIGHT + 64;
}

- (void)setupHeaderView
{
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = RGB(240, 244, 243);
    headerView.frame = CGRectMake(0, 0, kScreenWidth, 40);
    self.tableView.tableHeaderView = headerView;
    
    NSArray *titleArray = @[@"序列号", @"设备型号", @"智慧气感监测", @"状态"];
    CGFloat itemW = kScreenWidth * 0.25;
    for (int i = 0; i < titleArray.count; i++) {
        
        UILabel *numberLbl = [[UILabel alloc] init];
        numberLbl.numberOfLines = 2;
        numberLbl.text = titleArray[i];
        numberLbl.textColor = MAINColor;
        numberLbl.font = VRFont(14);
        numberLbl.textAlignment = NSTextAlignmentCenter;
        numberLbl.size = CGSizeMake(itemW, 40);
        numberLbl.centerX = itemW * (0.5 + i);
        [headerView addSubview:numberLbl];
        
        if (i < titleArray.count - 1) {
            
            UIView *vLineView = [[UIView alloc] init];
            vLineView.backgroundColor = MAINColor;
            vLineView.size = CGSizeMake(1, 24);
            vLineView.centerX = itemW * (1 + i);
            vLineView.centerY = 20;
            [headerView addSubview:vLineView];
        }
        
        if (i == 2) {
            self.typeTitleLbl = numberLbl;
        }
    }
}

- (void)setupListMenu
{
    
    UIView *listBgView = [[UIView alloc] init];
    listBgView.backgroundColor = RGB(255, 255, 255);
    listBgView.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT, kScreenWidth, 64);
    [self.view addSubview:listBgView];
    
    UIView *listView = [[UIView alloc] init];
    listView.layer.borderColor = MAINColor.CGColor;
    listView.layer.borderWidth = 1.0;
    listView.frame = CGRectMake(0, 0, kScreenWidth - 20 * 2, 40);
    listView.center = CGPointMake(kScreenWidth * 0.5, 32);
    [listBgView addSubview:listView];
    
    UILabel *detailLbl = [[UILabel alloc] init];
    detailLbl.text = @"数据类型选择";
    detailLbl.textColor = RGB(80, 80, 80);
    detailLbl.font = VRFont(14);
//    detailLbl.textAlignment = NSTextAlignmentRight;
    detailLbl.frame = CGRectMake(8, 0, 100, 40);
    [listView addSubview:detailLbl];
    
    //列表展示的模型
    NSMutableArray *section1Datas = [NSMutableArray array];
    
//    NSArray *title1Array = @[@"智慧烟感监测", @"智慧气感监测"];
//    for (NSString *str in title1Array) {
//        XMenuModel *model = [[XMenuModel alloc] init];
//        model.name = str;
//        model.id = 0;
//        [section1Datas addObject:model];
//    }
    
    NSMutableArray *allDatas = [NSMutableArray array];
    [allDatas addObject:section1Datas];
    
    //上边按钮展示的模型,此模型对text赋值即可.
    NSArray *titleArray = @[@"全部分类"];
    
    //若列表展示内容与按钮展示内容相同则showArr传nil即可。
    XDropMenuView *dropMenu = [[XDropMenuView alloc] initWithFrame:CGRectMake(0, 0, listView.width, 40) showOnView:self.view AllDataArr:allDatas showArr:[titleArray copy]];
    dropMenu.itemNormalColor = MAINColor;
    dropMenu.itemSelectedColor = MAINColor;
    dropMenu.titleNormalColor = MAINColor;
    dropMenu.titleSelectedColor = MAINColor;
    dropMenu.fonteSize = 14;
    dropMenu.delegate = self;
    [listView addSubview:dropMenu];
    self.dropMenu = dropMenu;
    
//    NSInteger index = self.curSelectBallIndex - 1;
//    if (index >= 0 && index < DropMenuTitle0.count) {
//
//        [dropMenu selectClum:0 Row:index];
//        self.class1 = DropMenuTitle1[index][@"param"];
//    }else{
//
//        [dropMenu selectClum:0 Row:0];
//        self.class1 = DropMenuTitle1[0][@"param"];
//    }
//
//    [dropMenu selectClum:1 Row:0];
//    self.class0 = DropMenuTitle0[0][@"param"];
    
}


#pragma mark - networking
- (void)getDeviceType
{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"UserID"] = [VRUser shareUser].token;
    param[@"fIsAreaManage"] = @([VRUser shareUser].fIsAreaManage);
    
    weakify(self)
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //    hud.bezelView.backgroundColor = RGBA(0, 0, 0, 0.9);
    //    hud.bezelView.style = MBProgressHUDBackgroundStyleBlur;
    hud.mode = MBProgressHUDModeIndeterminate;
    [JKNetworking postWithSign:GetDeviceType parameters:param success:^(NSDictionary *responseObject) {
        
        strongify(self)
        NSLog(@"responseObject:%@", responseObject);
        NSInteger code = [responseObject[@"state"] integerValue];
        NSString *msg = responseObject[@"message"];
        
        if (code == 0) {
            
//            self.deviceDataModelArray = [VRDeviceDataModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            NSMutableArray *section1Datas = [NSMutableArray array];
            NSArray *dictArray = responseObject[@"data"];
            for (NSDictionary *dict in dictArray) {
                
                XMenuModel *model = [[XMenuModel alloc] init];
                model.name = dict[@"fTypeName"];
                model.id = dict[@"fType"];
                [section1Datas addObject:model];
            }
            NSMutableArray *allDatas = [NSMutableArray array];
            [allDatas addObject:section1Datas];
            
            if (section1Datas.count > 0) {
                [self getDeviceDataWithType:[section1Datas.firstObject id]];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self.dropMenu.AllDataArr = allDatas;
                if(section1Datas.count){
                    [self.dropMenu selectClum:0 Row:0];
                }
                [hud hideAnimated:YES];
            });
        }else if(msg.length > 0){
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showErrorWithStatus:msg];
                [hud hideAnimated:YES];
            });
        }
    } error:^(NSError * _Nonnull error) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:@"未知错误"];
            [hud hideAnimated:YES];
            [self.tableView.mj_header endRefreshing];
        });
    }];
}


- (void)getDeviceDataWithType:(NSString *)type
{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"UserID"] = [VRUser shareUser].token;
    param[@"fIsAreaManage"] = @([VRUser shareUser].fIsAreaManage);
    param[@"type"] = type;
    
    weakify(self)
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //    hud.bezelView.backgroundColor = RGBA(0, 0, 0, 0.9);
    //    hud.bezelView.style = MBProgressHUDBackgroundStyleBlur;
    hud.mode = MBProgressHUDModeIndeterminate;
    [JKNetworking postWithSign:GetDeviceData parameters:param success:^(NSDictionary *responseObject) {
        
        strongify(self)
        NSLog(@"responseObject:%@", responseObject);
        NSInteger code = [responseObject[@"state"] integerValue];
        NSString *msg = responseObject[@"message"];
        
        if (code == 0) {
            
            self.deviceDataModelArray = [VRDeviceDataModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.tableView reloadData];
                [hud hideAnimated:YES];
            });
        }else if(msg.length > 0){
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showErrorWithStatus:msg];
                [hud hideAnimated:YES];
            });
        }
        [self.tableView.mj_header endRefreshing];
    } error:^(NSError * _Nonnull error) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:@"未知错误"];
            [hud hideAnimated:YES];
            [self.tableView.mj_header endRefreshing];
        });
    }];
}


#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 0.01;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.deviceDataModelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 42;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    VRDeviceDataCell *cell = [VRDeviceDataCell cellWith:tableView];
    self.deviceDataModelArray[indexPath.row].number = indexPath.row + 1;
    cell.model = self.deviceDataModelArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - XDropMenuViewDelegate
- (void)dropMenuView:(XDropMenuView *)dropMenuView didSelectRowAtColum:(NSInteger)colum Row:(NSInteger)row model:(XMenuModel *)model
{
    self.typeTitleLbl.text = model.name;
    [self getDeviceDataWithType:model.id];
}

#pragma mark - lazy
- (UITableView *)tableView
{
    if (_tableView == nil) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT + 64, kScreenWidth, kScreenHeight - NAVIGATION_BAR_HEIGHT - 64) style:UITableViewStylePlain];
        _tableView.backgroundColor = ScrollViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = [[UIView alloc] init];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)];
        _tableView.tableFooterView = view1;
//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        _tableView.estimatedRowHeight = 0;
//        _tableView.estimatedSectionHeaderHeight = 0;
//        _tableView.estimatedSectionFooterHeight = 0;
//        _tableView.sectionFooterHeight = 0.01;
//        [self.view addSubview:self.tableView];
//        //        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        //            make.left.right.bottom.mas_equalTo(self.view);
//        //            make.top.mas_equalTo(0);
//        //        }];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

@end
